package com.igoal.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Entity
@Table(name = "comment_entry")
@Searchable
public class CommentEntry extends BaseCommentEntry<CommentEntry, UserGoalEntry>
		implements ICommentEntry<UserGoalEntry>, Serializable {
	private static final long serialVersionUID = 8153432756613069795L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="entry_id")
	public UserGoalEntry getEntry() {
		return entry;
	}

	public void setEntry(UserGoalEntry entry) {
		this.entry = entry;
	}	
	
	@ManyToOne
	@JoinColumn(name="parent_id", nullable=true)
	public CommentEntry getParent() {
		return parent;
	}

	public void setParent(CommentEntry parent) {
		this.parent = parent;
	}
	
	@OneToMany(mappedBy="parent", fetch=FetchType.LAZY)	
	public Set<CommentEntry> getChild() {
		return child;
	}

	public void setChild(Set<CommentEntry> child) {
		this.child = child;
	}

	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="user_id", nullable=true)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Column(nullable=true)
	@SearchableProperty
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	@Column(nullable=true)
	@SearchableProperty
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="user_ip", nullable=true)
	public String getUserIP() {
		return userIP;
	}

	public void setUserIP(String userIP) {
		this.userIP = userIP;
	}
	
	@Column(name="num_children", nullable=false)
	public Integer getChildrenCount() {
		return childrenCount;
	}

	public void setChildrenCount(Integer childrenCount) {
		this.childrenCount = childrenCount;
	}
	
	@Column(nullable=false)
	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}
	
	@Column(nullable=false)
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Column(nullable=true)
	@SearchableProperty
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}	
 
    public int hashCode() {
    	int hashCode = getId().hashCode() + getCreateDate().hashCode();
        return hashCode;
    }

	@Override
	public String toString() {
		if(getComment() != null){
			return getComment().toString();
		}
		return null;
	}
	
	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		CommentEntry that = (CommentEntry) o;

		if (getId() != null ? !getId().equals(that.getId())
				: that.getId() != null)
			return false;

		return true;
	}

	public void setParent(ICommentEntry<UserGoalEntry> parent) {
		CommentEntry parentEntry = (CommentEntry) parent;
		setParent(parentEntry);
	}
}
